{{>header}}

<div class="edit main" id="edit">
<form action="/article/edit/{{post._id}}" method="post">
    标题：<input value="{{ post.title }}" name="title" type="text" /><br><br>

    标签：
    {{#each tags}}
    <input type="checkbox" name="tags" value="{{name}}"{{#if checked}} checked{{/if}}/>{{name}}&nbsp;
    {{/each}}
    <br><br>

    简介（支持HTML标签(a,strong,em)）：<br/>
    <textarea name="intro">{{post.intro}}</textarea><br/><br>


    正文：<br/>
    <textarea name="content">{{ post.content }}</textarea><br/><br/>

    <input type="submit" value="提交"/>
    <span id="errorMsg" style="color:#c00;display: none;"></span>
</form>
</div>

{{>editSide}}

{{#section 'script'}}
<script>
    $(function(){
        var limit={
            title:[{{postCharacterLengthLimit.title}}],
            intro:[{{postCharacterLengthLimit.intro}}],
            content:[{{postCharacterLengthLimit.content}}]
        };
        var $errorMsg=$('#errorMsg');
        $('#edit').on('submit','form',function(){
            var errors=[];
            var title=$(this).find('[name="title"]').val();
            var intro=$(this).find('[name="intro"]').val();
            var content=$(this).find('[name="content"]').val();

            if(title)title=title.replace(/^\s+|\s+$/g,'');
            if(intro)intro=intro.replace(/^\s+|\s+$/g,'');
            if(content)content=content.replace(/^\s+|\s+$/g,'');

            if(!title || title.length<limit.title[0] || title.length>limit.title[1])errors.push(['标题内容长度非法(',limit.title[0],'~',limit.title[1],')'].join(''));
            if(!intro || intro.length<limit.intro[0] || intro.length>limit.intro[1])errors.push(['简介内容长度非法(',limit.intro[0],'~',limit.intro[1],')'].join(''));
            if(!content || content.length<limit.content[0] || content.length>limit.content[1])errors.push(['正文内容长度非法(',limit.content[0],'~',limit.content[1],')'].join(''));

            if(!$(this).find('[name="tags"]:checked').length)errors.push('标签未选择');
            if(errors.length){
                $errorMsg.text(errors.join()).show();
                return false;
            }
        });
    });
</script>
{{/section}}